package com.substation.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.bean.LoginInfo;
import com.interfaces.NovaCallback;
import com.substation.R;
import com.substation.utils.DialogUtils;
import com.substation.utils.NetUtils;
import com.substation.utils.T;

import butterknife.ButterKnife;
import kr.co.namee.permissiongen.PermissionGen;

/**
 * Created by admin on 2018/5/14.
 */

public abstract class BaseActivity extends AppCompatActivity implements NovaCallback {


    protected Context context;
    private boolean isPause;
    private AlertDialog dialog;
    protected int errCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(getLayoutId());
        super.onCreate(savedInstanceState);
        context = this;
        if (!NetUtils.isConnected(this))//检查网络是否连接
        {
            T.showShort(this, "网络未连接，请检查网络");
        }
        ButterKnife.bind(this);
        initView();
    }

    protected abstract int getLayoutId();

    protected abstract void initView();



    @Override
    protected void onPause() {
        super.onPause();
        isPause = true;
    }



    @Override
    protected void onResume() {
        super.onResume();
        initData();
        isPause = false;
    }

    protected void initData() {
    }

    /**
     * 消息提示
     *
     * @param msg
     */
    public void showMsg(String msg) {
        if(!isPause){
            if(dialog==null){
                AlertDialog.Builder builder=new AlertDialog.Builder(this, R.style.dialogStyle);
                dialog=builder.setTitle("提示").setMessage(msg).setPositiveButton("确定",null).show();
            }else{
                dialog.cancel();
                dialog.setTitle("提示");
                dialog.setMessage(msg);
                dialog.show();
            }
        }
    }

    @Override
    public void err(int i,int status, String err) {
        if(status==1003){
            LoginInfo.remove(context);
            startActivity(new Intent(context,LoginActivity.class));
            T.showLong(context,err);
            finish();
        }else{
            DialogUtils.showMsgDialog(this,"错误提示",err);
        }
    }

    @Override
    public void success(int i, String obj) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}
